package aqv

import grails.plugin.springsecurity.annotation.Secured
import static org.springframework.http.HttpStatus.*
import grails.transaction.Transactional

//@Transactional(readOnly = true)
class PoliticianController {
	
	static scaffold = Politician
	
	def springSecurityService

    /*static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]

    def index(Integer max) {
        params.max = Math.min(max ?: 10, 100)
        respond Politician.list(params), model:[politicianInstanceCount: Politician.count()]
    }

    def show(Politician politicianInstance) {
        respond politicianInstance
    }*/
	
	
	@Secured(['ROLE_ADMIN'])
	def approveUpdate() {
		
		render "test"
	}
	

	@Secured(['ROLE_ADMIN'])
    def create() {
		
		println "Enters create Politician"
		
		def user = springSecurityService.isLoggedIn() ? springSecurityService.loadCurrentUser() : null
		params.selectedPoliticalPartyID = user.politicalParty.id
		
		println "Leaves create Politician"
		
		respond new Politician(params)
		
//		if (session.user != null) {
//			respond new Politician(params)
//		} else {
//			flash.message = message(code: 'error.permission.createPolitician.notLogged')
//			render(view: "index")
//		}
    }

    @Transactional
	@Secured(['ROLE_ADMIN'])
    def save(Politician politicianInstance) {
		
		println "Enters save Politician"
		
		def user = springSecurityService.isLoggedIn() ? springSecurityService.loadCurrentUser() : null
		params.selectedPoliticalPartyID = user.politicalParty.id
		
		
        if (politicianInstance == null) {
            notFound()
            return
        }

        if (politicianInstance.hasErrors()) {
            respond politicianInstance.errors, view:'create'
            return
        }

        politicianInstance.save flush:true
		
		println "Leaves save Politician"

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.created.message', args: [message(code: 'politicianInstance.label', default: 'Politician'), politicianInstance.id])
                redirect politicianInstance
            }
            '*' { respond politicianInstance, [status: CREATED] }
        }
    }

	
	@Secured(['ROLE_USER', "authentication.confirm"])
    def edit(Politician politicianInstance) {
		
		println "Enters edit Politician"
		
		// here could be a validation of same political party id
        respond politicianInstance
		
		println "Leaves edit Politician"
    }

    @Transactional
	@Secured(['ROLE_USER', "authentication.confirm"])
    def update(Politician politicianInstance) {
		
		println "Enters update Politician"
		
		def oldPolitician = Politician.get(params.id)
		def oldPolitician2 = Politician.load(params.id)
		def user = springSecurityService.isLoggedIn() ? springSecurityService.loadCurrentUser() : null
		
		
		println "oldPolitician name : " + oldPolitician.name
		println "oldPolitician2 name : " + oldPolitician2.name
		
		
		// validation of same political party
		if (user.politicalParty.id == oldPolitician.politicalParty.id ) {
			
	        if (politicianInstance == null) {
	            notFound()
	            return
	        }
	
	        if (politicianInstance.hasErrors()) {
	            respond politicianInstance.errors, view:'edit'
	            return
	        }
	
			
			
			// THIS SHOULDNT BE A SAVE AS THE UPDATE SHOULD BE SAVED IN A TEMPORALLY TABLE
	        politicianInstance.save flush:true
	
			
			
			
			
			println "Leaves update Politician"
			
			
	        request.withFormat {
	            form multipartForm {
	                flash.message = message(code: 'default.updated.message', args: [message(code: 'Politician.label', default: 'Politician'), politicianInstance.id])
	                redirect politicianInstance
	            }
	            '*'{ respond politicianInstance, [status: OK] }
	        }
		} else {

			println "Leaves update Politician"
			flash.message = message(code: 'error.permission.modifyPolitician', args: [oldPolitician.politicalParty.name])
			render(view: "show", model: [politicianInstance: politicianInstance])
			//respond politicianInstance
		}
    }

    /*@Transactional
    def delete(Politician politicianInstance) {

        if (politicianInstance == null) {
            notFound()
            return
        }

        politicianInstance.delete flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.deleted.message', args: [message(code: 'Politician.label', default: 'Politician'), politicianInstance.id])
                redirect action:"index", method:"GET"
            }
            '*'{ render status: NO_CONTENT }
        }
    }

    protected void notFound() {
        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.not.found.message', args: [message(code: 'politicianInstance.label', default: 'Politician'), params.id])
                redirect action: "index", method: "GET"
            }
            '*'{ render status: NOT_FOUND }
        }
    }*/
}
